﻿using System;
using System.Collections.Generic;

using ComponentFactory.Krypton.Toolkit;

using ExtensibleEventViewer.Console.Forms;
using ExtensibleEventViewer.Console.Presenters;
using ExtensibleEventViewer.Console.Views;


namespace ExtensibleEventViewer.Console.Controls
{
	public partial class ComputerListFilter : DecoratedUserControl, IComputerListView
	{
		/// <summary>
		/// Occurs when selected computer is changed
		/// </summary>
		public event Action<string> SelectedComputerChanged;

		private readonly ComputerListPresenter _presenter;

		private string _lastSelectedComputerName;

		/// <summary>
		/// Initializes the control
		/// </summary>
		public ComputerListFilter()
		{
			InitializeComponent();

			_presenter = new ComputerListPresenter(this);

			ddlComputerNameList.SelectedValueChanged += SelectedComputerNameChanged;
		}

		protected override void OnLoad(EventArgs e)
		{
			if (!DesignMode)
			{
				_presenter.LoadData();
			}

			base.OnLoad(e);
		}

		public IEnumerable<string> ComputerList
		{
			get
			{
				foreach (string item in ddlComputerNameList.Items)
				{
					yield return item;
				}
			}
			set
			{
				if (ddlComputerNameList.InvokeRequired)
				{
					Action populateComputerList = () => PopulateComputerList(value);

					ddlComputerNameList.Invoke(populateComputerList);
				}
				else
				{
					PopulateComputerList(value);
				}
			}
		}

		public string SelectedComputerName
		{
			get
			{
				return ddlComputerNameList.SelectedItem as string;
			}
		}

		private void SelectedComputerNameChanged(object sender, EventArgs e)
		{
			if (!string.Equals(_lastSelectedComputerName, ddlComputerNameList.SelectedItem as string))
			{
				_lastSelectedComputerName = ddlComputerNameList.SelectedItem as string;

				if (SelectedComputerChanged != null)
				{
					SelectedComputerChanged(ddlComputerNameList.SelectedItem as string);
				}
			}
		}

		private void PopulateComputerList(IEnumerable<string> computers)
		{
			ddlComputerNameList.BeginUpdate();

			ddlComputerNameList.Items.Clear();

			foreach (string computerName in computers)
			{
				ddlComputerNameList.Items.Add(computerName);
			}

			if (ddlComputerNameList.Items.Count > 0)
			{
				ddlComputerNameList.SelectedIndex = 0;
			}

			ddlComputerNameList.EndUpdate();
		}

		private void AddComputerCommandExecute(object sender, EventArgs e)
		{
			using (AddComputerWnd addComputerWnd = new AddComputerWnd())
			{
				addComputerWnd.AddComputer += AddComputer;

				addComputerWnd.ShowDialog(this);
			}
		}

		private void EditComputerListCommandExecute(object sender, EventArgs e)
		{
			using(ComputerListWnd computerListWnd = new ComputerListWnd(ComputerList))
			{
				computerListWnd.SaveComputerList += PopulateComputerList;

				computerListWnd.ShowDialog(this);
			}
		}

		private void AddComputer(string computerName)
		{
			bool isComputerNameInList = false;

			foreach (string item in ddlComputerNameList.Items)
			{
				if (string.Equals(item, computerName))
				{
					isComputerNameInList = true;
					break;
				}
			}

			if (!isComputerNameInList)
			{
				ddlComputerNameList.BeginUpdate();
				
				ddlComputerNameList.Items.Add(computerName);

				if (ddlComputerNameList.Items.Count == 1)
				{
					ddlComputerNameList.SelectedIndex = 0;
				}

				ddlComputerNameList.EndUpdate();
			}
		}

		public override PaletteMode PaletteMode
		{
			set
			{
				ddlComputerNameList.PaletteMode = value;
			}
		}
	}
}
